from django.db import models
from django.contrib.auth.models import User


class Notification(models.Model):
    """通知模型"""
    
    NOTIFICATION_TYPE_CHOICES = [
        ('order', '订单通知'),
        ('comment', '评论通知'),
        ('like', '点赞通知'),
        ('follow', '关注通知'),
        ('system', '系统通知'),
        ('price_change', '价格变动'),
        ('group', '小组通知'),
    ]
    
    id = models.BigAutoField(
        primary_key=True,
        db_comment='通知ID'
    )
    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='notifications',
        db_comment='接收用户ID'
    )
    notification_type = models.CharField(
        max_length=20,
        choices=NOTIFICATION_TYPE_CHOICES,
        db_index=True,
        db_comment='通知类型'
    )
    title = models.CharField(
        max_length=200,
        db_comment='通知标题'
    )
    content = models.TextField(
        db_comment='通知内容'
    )
    related_object_type = models.CharField(
        max_length=50,
        null=True,
        blank=True,
        db_comment='关联对象类型'
    )
    related_object_id = models.BigIntegerField(
        null=True,
        blank=True,
        db_comment='关联对象ID'
    )
    extra_data = models.JSONField(
        null=True,
        blank=True,
        db_comment='额外数据'
    )
    is_read = models.BooleanField(
        default=False,
        db_index=True,
        db_comment='是否已读'
    )
    read_at = models.DateTimeField(
        null=True,
        blank=True,
        db_comment='阅读时间'
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        db_index=True,
        db_comment='创建时间'
    )
    
    class Meta:
        db_table = 'campus_notification'
        db_table_comment = '系统通知表'
        verbose_name = '通知'
        verbose_name_plural = '通知'
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['user', 'is_read', 'created_at']),
            models.Index(fields=['notification_type', 'created_at']),
        ]
    
    def __str__(self):
        return f'{self.user.username} - {self.title}'
    
    def mark_as_read(self):
        """标记为已读"""
        from django.utils import timezone
        self.is_read = True
        self.read_at = timezone.now()
        self.save(update_fields=['is_read', 'read_at'])


class NotificationSettings(models.Model):
    """通知设置"""
    
    id = models.BigAutoField(
        primary_key=True,
        db_comment='设置ID'
    )
    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        related_name='notification_settings',
        db_comment='用户ID'
    )
    order_notifications = models.BooleanField(
        default=True,
        db_comment='订单通知开关'
    )
    comment_notifications = models.BooleanField(
        default=True,
        db_comment='评论通知开关'
    )
    like_notifications = models.BooleanField(
        default=True,
        db_comment='点赞通知开关'
    )
    follow_notifications = models.BooleanField(
        default=True,
        db_comment='关注通知开关'
    )
    system_notifications = models.BooleanField(
        default=True,
        db_comment='系统通知开关'
    )
    price_change_notifications = models.BooleanField(
        default=True,
        db_comment='价格变动通知开关'
    )
    group_notifications = models.BooleanField(
        default=True,
        db_comment='小组通知开关'
    )
    email_notifications = models.BooleanField(
        default=False,
        db_comment='邮件通知开关'
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        db_comment='创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True,
        db_comment='更新时间'
    )
    
    class Meta:
        db_table = 'campus_notification_settings'
        db_table_comment = '用户通知设置表'
        verbose_name = '通知设置'
        verbose_name_plural = '通知设置'
    
    def __str__(self):
        return f'{self.user.username}的通知设置'
